package com.project.service;

import com.project.dao.KpointMapper;
import com.project.entity.Kpoint;
import com.project.entity.User;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.project.common.utils.MyPageHelper;

import java.util.ArrayList;
import java.util.List;

@Transactional(rollbackFor = Exception.class)
@Service
public class KpointServiceImpl extends BaseService implements KpointService {

    @Autowired
    KpointMapper kpointMapper;

    @Override
    public void add(Kpoint kpoint) {
        kpointMapper.insertSelective(kpoint);
    }

    @Override
    public List<Kpoint> findByCondition(Kpoint kpoint) {
        MyPageHelper.setPageParam(kpoint);
        return kpointMapper.selectByCondition(kpoint);
    }

    @Override
    public Kpoint findById(Integer id) {
        return kpointMapper.selectByPrimaryKey(id);
    }

    @Override
    public Kpoint findByName(String name) {
        return kpointMapper.selectByName(name);
    }

    @Override
    public void removeById(Integer id) {
        kpointMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int modify(Kpoint kpoint) {
        Kpoint oldKpoint = findById(kpoint.getId());
        return kpointMapper.updateById(kpoint);
    }

    @Override
    public List<Kpoint> queryListByPid(Integer pid) {
        List<Kpoint> kpointList = kpointMapper.selectByPid(pid);
        if (kpointList.size()==0){
            return null;
        }else {
            for (Kpoint kpoint : kpointList) {
                List<Kpoint> childList = queryListByPid(kpoint.getId());
                kpoint.setChildren(childList);
            }
        }
        return kpointList;
    }

    @Override
    public int changenamebyid(Integer id, String name) {
        return kpointMapper.changeNameById(id, name);
    }
}
